'use strict';
define(function(require, exports, module) {

	var app = angular.module('myApp', ['ui.router']);
	app.config(['$controllerProvider', '$compileProvider', '$filterProvider', '$provide', '$stateProvider', '$urlRouterProvider', function($controllerProvider, $compileProvider, $filterProvider, $provide, $stateProvider, $urlRouterProvider) {

		app.register = {
			controller: $controllerProvider.register,
			directive: $compileProvider.directive,
			filter: $filterProvider.register,
			factory: $provide.factory,
			service: $provide.service
		};

		app.loadMod = function(src) {
			return function($$q, $rootScope) {
				console.info("aaaaaa");
				var deferred = $$q.defer();
				var dependencies = src;

				require.async(dependencies, function(mod) {
					mod.init(app);

					$rootScope.$apply(function() {
						deferred.resolve();
					});
				});
				return deferred.promise;
			};
		};

		var arr = [{
			state: "login",
			label: "登录"
		},{
			state: "register",
			label: "注册"
		}];


		var states = {};

		angular.forEach(arr, function(item) {
			// 处理多级state，自动添加各个父级state
			var paths = item.state.split(/\./g);
			var currentPath = '';
			angular.forEach(paths, function(path) {
				currentPath += path;
				states[currentPath] = item;
				currentPath += '.'
			});
		});

		// 遵循约定优于配置的原则自动批量注册路由
		angular.forEach(states, function(item, state) {
			var path = state.replace(/\./g, '/');
			var lastState = state.match(/(\w+)$/)[0];
			$stateProvider.state(state, {
				url: '/' + lastState,
				templateUrl: 'app/' + path + '/view/' + lastState + '.html',
				resolve: {
					load: app.loadMod('./' + path + '/mod.js')
				}
			});
		});

	}]);



	// var sRouter = require('../common/js/srouter');

	// sRouter.stateRoute(app, 'login', '/login', 'app/login/view/login.html', '../../app/login/js/loginController');
	// sRouter.stateRoute(app, 'register', '/register', 'app/register/view/register.html', '../../app/register/js/registerController');

	angular.bootstrap(document.body, ['myApp']);

	console.info("app starting...");

});